#if 0
#include <stdio.h>
#include <string.h>
#include <string>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>//涉及到存放IP地址和端口号和家族协议的结构体类型
#include <stdlib.h>
#include <pthread.h>

#include "CJsonObject.hpp"

using namespace std;

bool sendHttpJsonFunc(int socketFd);


const int CONST_SERVER_PORT = 1998;
const std::string CONST_SERVER_IPADDR = "192.168.165.105";

//为每次通信都构建一个结构体
typedef struct client_info
{
	//存放线程号，套接字，IP地址
	//pthread_t t_id;
	int socketfd;
	struct in_addr serverIp;	// IP
}CLIENT_INFO_STU;

// 
void *recvThreadFunc(void *arg)
{
    CLIENT_INFO_STU serverInfoStu;
    serverInfoStu = *(CLIENT_INFO_STU *)arg;
    char recvbuf[100];
    //int len = sizeof(c_addr);
    while(1)
    {
        bzero(&recvbuf,sizeof(recvbuf));
        //read(c_fd,recvbuf,sizeof(recvbuf));
        //int ret = recvfrom(c_fd,recvbuf,strlen(recvbuf),0,(struct sockaddr *)&c_addr,&len);
        int ret = recv(serverInfoStu.socketfd,recvbuf,sizeof(recvbuf),0);
        if(ret <=0)
        {
            printf("disconnect, close socket Fd, exit\n");
			close(serverInfoStu.socketfd);
			exit(-1);            
        }
        printf("Message from %s : %s\n",inet_ntoa(serverInfoStu.serverIp),recvbuf);
        //printf("%s",recvbuf);
    }
}

int main()
{
	//================socket()
	int socketFd = socket(AF_INET,SOCK_STREAM,0);
	if(socketFd == -1)
	{
		printf("socket error\n");
		exit(-1);
	}
		
	//定义一个结构体来存放服务器的信息，用于bind()
	struct sockaddr_in ServerInfo;
	bzero(&ServerInfo,sizeof(ServerInfo));
	
	ServerInfo.sin_family = AF_INET;				//协议
	ServerInfo.sin_port = htons(CONST_SERVER_PORT);				//端口号
	ServerInfo.sin_addr.s_addr = inet_addr("192.168.1.145");

	
	//================connect()
	int ret = connect(socketFd,(struct sockaddr *)&ServerInfo,sizeof(ServerInfo));
	if(ret == -1)
	{
		printf("connect error\n");
		exit(-1);
	}

#if 0
    CLIENT_INFO_STU serverInfoStu;

    //存放本次通信的套接字
    serverInfoStu.socketfd = socketFd;    
    serverInfoStu.serverIp.s_addr = ServerInfo.sin_addr.s_addr;


    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

    pthread_t recvThread;
    pthread_create(&recvThread,&attr,recvThreadFunc,(void*)&serverInfoStu);

    // send to server, with input string
    //sendInputFunc(socketFd);

#else

        
    sendHttpJsonFunc(socketFd);
        
    
    

#endif
    printf("close()\n");
	close(socketFd);	
}


void sendInputFunc(int socketFd)
{
	char sendbuf[100];
	while(1)
	{
		bzero(&sendbuf,sizeof(sendbuf));
		printf("input something\n");
		fgets(sendbuf,sizeof(sendbuf),stdin);
		//write(c_fd,sendbuf,sizeof(sendbuf));
		
		//ret = sendto(c_fd,sendbuf,strlen(sendbuf),0,(struct sockaddr *)&c_addr,sizeof(c_addr));
		int ret = send(socketFd,sendbuf,sizeof(sendbuf),0);
		if(ret == -1)
		{
			printf("sendto error\n");
			exit(-1);
		}
		//printf("ret = %d\n",ret);
	}
}


bool sendHttpJsonFunc(int socketFd)
{
    while(1)
    {
        printf("Client ----- sendHttpJsonFunc() socketFd = %d\n", socketFd);
        // 发送HTTP请求
        std::string strRequest;
        strRequest +="GET / HTTP/1.1\r\n";
        strRequest += "Host: " + CONST_SERVER_IPADDR + ":" + to_string(CONST_SERVER_PORT) + "\r\n";
        strRequest += "Accept: */*\r\n";
        strRequest += "Connection: close\r\n";
        strRequest += "\r\n";

        //strRequest += "{\"Data\":{\"Name\":\"Leo\",\"Age\":26,\"Gender\":\"Male\",\"City\":\"Guangzhou\"}}";
        neb::CJsonObject reqJsonObj;
        reqJsonObj.AddEmptySubObject("Data");
        neb::CJsonObject &dataJsonObj = reqJsonObj["Data"];
        dataJsonObj.Add("Name", "Leo");
        dataJsonObj.Add("Age", 26);
        dataJsonObj.Add("Gender", "Male");
        dataJsonObj.Add("City", "Guangzhou111");
        

        std::string strTest = reqJsonObj.ToString();

        neb::CJsonObject rootJsonObj(strTest);

        strRequest += reqJsonObj.ToString();

        printf("Client ----- send string %s\n", strRequest.c_str());
        if (send(socketFd, strRequest.c_str(), strRequest.length(), 0) == -1) 
        {
            printf("Error: Failed to send HTTP request.\n");
            return false;
        }
        printf("111111111111111111111111\n");

        // 接收HTTP响应
        std::string strResponse;
        char buffer[1024];
        int bytes_received;
        while ((bytes_received = recv(socketFd, buffer, sizeof(buffer), 0)) > 0) 
        {
            strResponse += buffer;
        }
        if (bytes_received == -1) 
        {
            printf("Error: Failed to receive HTTP response.\n");
            return false;
        }
        printf("2222222222222222222222222222\n");

        printf("Client ----- bytes_received = %d, JSON strResponse: = %s\n",bytes_received, strResponse.c_str());

        // 解析JSON响应
        size_t json_start = strResponse.find("{");
        size_t json_end = strResponse.rfind("}");
        if (json_start == string::npos || json_end == string::npos || json_end < json_start) 
        {
            printf("Error: Invalid JSON response.  data = \n%s\n", strResponse.c_str());
            return false;
        }
        string json_string = strResponse.substr(json_start, json_end - json_start + 1);
        printf("Client ----- JSON response:  %s\n",json_string.c_str());
    }

}

#endif